﻿Imports Runa.Foundation
Imports Runa.Resources.ContentReader
Imports Runa.Resources.ContentReader.VectorFont
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics

Namespace Global.Runa.Resources.ResourceManager

    Public Class VectorFontObject
        Inherits ResourceObjectBase

        Public Property Size() As Integer
        Public Property Color() As Color
        Public Property Font() As VectorFont

        Public Sub New()
            MyBase.New()
            Me.Size = 0
            Me.Color = Color.Black
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String)
            MyBase.New(name, fontFileName)
            Me.Size = 22
            Me.Color = Color.Black
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal g As Game)
            Me.New(name, fontFileName)
            Me.Load(g)
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal spf As VectorFont, Optional ByVal fontSize As Integer = 22)
            MyBase.New(name, fontFileName)
            Me.Font = spf
            Me.Size = fontSize
            Me.Color = Color.Black
            Me.IsLoaded = True
        End Sub

        Public Sub New(ByVal name As String, ByVal fontFileName As String, ByVal spf As VectorFont, ByVal color As Color, Optional ByVal fontSize As Integer = 22)
            MyBase.New(name, fontFileName)
            Me.Font = spf
            Me.Size = fontSize
            Me.Color = color
            Me.IsLoaded = True
        End Sub

        Public Overrides Function Load(g As Game) As Boolean
            If Me.IsLoaded Then Return True
            Me.Font = g.Content.Load(Of VectorFont)(Me.FileName)
            Return True
        End Function

        Public Function MeasureString(ByVal text As String, ByVal rect As Rectangle) As List(Of Tuple(Of Char, Vector2))
            'Dim lst As New List(Of Tuple(Of Char, Vector2))
            'Dim font = Me
            'Dim totalSize = (From c In text Select Me.Font.Characters(Me.Font.CharacterMap(c)).Faces.Count).Sum()
            'If totalSize.X > rect.Width Then
            '    Dim lines = CInt(totalSize.X / rect.Width + 0.5)
            '    Dim charCountPerLine =
            '        Function()
            '            Dim chCount As Integer = 0
            '            Dim deltaX As Integer = 0
            '            While deltaX < rect.X
            '                deltaX += font.Font.MeasureString(text(chCount)).X
            '                chCount += 1
            '            End While
            '            Return chCount
            '        End Function()
            '    Dim posX = 0
            '    Dim posY = 0
            '    Dim line = 0
            '    For i As Integer = 0 To text.Length - 1
            '        Dim count = i + 1
            '        Dim c = text(i)
            '        Dim index = count Mod charCountPerLine
            '        Dim v = font.Font.MeasureString(c.ToString())
            '        If index = 0 Then
            '            line += 1
            '            posX = 0
            '        Else
            '            posX += v.X
            '        End If
            '        posY = v.Y * line
            '        lst.Add(New Tuple(Of Char, Vector2)(c, New Vector2(posX, posY)))
            '    Next
            'Else
            '    Dim posX As Integer = 0
            '    Dim posY As Integer = 0
            '    For Each c In text
            '        Dim v = font.Font.MeasureString(c.ToString())
            '        posX += v.X
            '        lst.Add(New Tuple(Of Char, Vector2)(c, New Vector2(posX, posY)))
            '    Next
            'End If
            'Return lst
        End Function

        Public Shared Narrowing Operator CType(ByVal fo As VectorFontObject) As VectorFont
            Return fo.Font
        End Operator

    End Class

End Namespace